﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DLV.Data.Entities;
using DLV.FrameWork;
using DLV.Library.Utilities;

namespace DLV.BackEnd.Pages
{
    public partial class EditMenu : AdminPageBase
    {
        protected List<ViandInfo> ViandList = new List<ViandInfo>(); 

        private MenuInfo _menuInfo = new MenuInfo();
        private readonly MenuController _menuController = new MenuController();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(Request.QueryString["MenuId"]))
                _menuInfo = _menuController.GetMenu(int.Parse(Request.QueryString["MenuId"]));

            if (Page.IsPostBack) return;

            LoadRestaurant();
            LoadAdminUser();
            LoadMenuType();
            LoadStatus();

            ViandList = new ViandController().ListViand().OrderBy(p => p.ViandName).ToList();

            if (_menuInfo.MenuId != -1)
                RenderForm();
        }

        protected void lbtSave_Click(object sender, EventArgs e)
        {
            SaveData();
        }
        protected void lbtClose_Click(object sender, EventArgs e)
        {
            Response.Redirect("/Pages/ListMenu.aspx?xml=Menu");
        }

        private void SaveData()
        {
            _menuInfo.MenuName = txtMenuName.Text;
            _menuInfo.RestaurantId = int.Parse(ddlRestaurant.SelectedValue);
            _menuInfo.MenuTypeId = int.Parse(ddlMenuType.SelectedValue);
            _menuInfo.Viands = txtViands.Value;
            _menuInfo.SortDescription = txtSortDescription.Text;
            _menuInfo.DetailDescription = txtContent.Text;
            _menuInfo.Price = String.IsNullOrEmpty(txtPrice.Text) ? 0 : decimal.Parse(txtPrice.Text);
            _menuInfo.Status = int.Parse(ddlStatus.SelectedValue);

            if (_menuInfo.MenuId == -1)
            {
                _menuInfo.CreatedBy = ddlAdminUser.SelectedValue == "-1"
                    ? UserId
                    : int.Parse(ddlAdminUser.SelectedValue);
                _menuInfo.CreatedDate = DateTime.Now;
                _menuController.InsertMenu(_menuInfo);
            }
            else
            {
                _menuInfo.UpdatedBy = UserId;
                _menuInfo.UpdatedDate = DateTime.Now;
                _menuController.UpdateMenu(_menuInfo);
            }
            Response.Redirect("/Pages/ListMenu.aspx?xml=Menu");
        }

        private void RenderForm()
        {
            txtMenuName.Text = _menuInfo.MenuName;
            ddlRestaurant.SelectedValue = _menuInfo.RestaurantId.ToString();
            ddlMenuType.SelectedValue = _menuInfo.MenuTypeId.ToString();
            txtViands.Value = _menuInfo.Viands;
            txtSortDescription.Text = _menuInfo.SortDescription;
            txtPrice.Text = _menuInfo.Price.ToString();
            txtContent.Text = _menuInfo.DetailDescription;
            ddlStatus.SelectedValue = _menuInfo.Status.ToString();
            ddlAdminUser.SelectedValue = _menuInfo.CreatedBy.ToString();
        }

        private void LoadRestaurant()
        {
            ddlRestaurant.DataSource =
                new RestaurantController().ListRestaurant()
                    .Where(p => p.Status == (int) Globals.DocumentStatus.Use)
                    .OrderBy(p => p.RestaurantFullName).ToList();
            ddlRestaurant.DataTextField = "RestaurantFullName";
            ddlRestaurant.DataValueField = "RestaurantId";
            ddlRestaurant.DataBind();

            ddlRestaurant.Items.Insert(0, new ListItem("*", "-1"));
        }

        private void LoadMenuType()
        {
            ddlMenuType.DataSource = new MenuTypeController().ListMenuType()
                .Where(p => p.Status == (int)Globals.DocumentStatus.Use)
                .OrderBy(p => p.MenuTypeName);
            ddlMenuType.DataTextField = "MenuTypeName";
            ddlMenuType.DataValueField = "MenuTypeId";
            ddlMenuType.DataBind();

            ddlMenuType.Items.Insert(0, new ListItem("--- Chọn loại menu ---", "-1"));
        }

        private void LoadAdminUser()
        {
            ddlAdminUser.DataSource = new AdminUserController().ListAdminUser();
            ddlAdminUser.DataTextField = "Username";
            ddlAdminUser.DataValueField = "UserID";
            ddlAdminUser.DataBind();

            ddlAdminUser.Items.Insert(0, new ListItem("--- Chọn người tạo ---", "-1"));
        }

        private void LoadStatus()
        {
            var ds = new DataSet();
            ds.ReadXml(MapPath("~/xml/Status.xml"));
            var dv = ds.Tables["Status"].DefaultView;
            ddlStatus.DataTextField = "Name";
            ddlStatus.DataValueField = "ID";
            ddlStatus.DataSource = dv;
            ddlStatus.DataBind();
        }
    }
}